package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
public class READOMDDATA extends AxdrType {
    private ByteBuffer dataCode = null;
    private OMD omd = null;
    private Data pram = null;
    private OAD oad = null;
    private Unsigned8 readDelay = null;

    public READOMDDATA() {
    }

    public void setDataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(OMD omd, Data pram, OAD oad, Unsigned8 readDelay) {
        this.omd = omd;
        this.pram = pram;
        this.oad = oad;
        this.readDelay = readDelay;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.omd = new OMD();
        codeLength += this.omd.decode(input);
        this.pram = new Data();
        codeLength += this.pram.decode(input);
        this.oad = new OAD();
        codeLength += this.oad.decode(input);
        this.readDelay = new Unsigned8();
        codeLength += this.readDelay.decode(input);
        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.remaining();
            while (this.dataCode.hasRemaining()) {
                output.write(this.dataCode.get());
            }
        } else {
            if (this.readDelay != null && this.oad != null && this.pram != null && this.omd != null) {
                codeLength = 0;
                codeLength += this.readDelay.encode(output);
                codeLength += this.oad.encode(output);
                codeLength += this.pram.encode(output);
                codeLength += this.omd.encode(output);
            } else {
                throw new Exception("readDelay || oad || pram || omd is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.toByteArray());
    }

    @Override
    public String toString() {
        if (this.readDelay != null && this.oad != null && this.pram != null && this.omd != null) {
            return "sequence: {" + "omd: " + this.omd + ", pram: " + this.pram + ", oad: " + this.oad + ", readDelay: " + this.readDelay + "}";
        } else {
            return "readDelay || oad || pram || omd is null";
        }
    }
}



